<template>
  <div class="role-container">
    <!-- 左侧角色列表 -->
    <div class="left-wrapper">
      <div
        v-for="(item,index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active:index === activeIndex}"
        @click="changeClass(index,item.roleId)"
      >
        <div class="role-info">
          <svg-icon icon-class="user" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">

          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-edit-outline"> <span @click="$router.push(`/addRole?roleId=${item.roleId}`)">编辑角色</span></el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete"><span @click="edlRolefn(item.roleId)">删除角色</span></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>
    <!-- 右侧权限和成员 -->
    <div class="right-wrapper">
      <div class="tree-wrapper">
        <!-- 循环每一个管理页面 -->
        <el-tabs v-model="activeName" class="myELTables">

          <el-tab-pane label="用户管理" name="roleAdministration" class="myElTabPane">
            <div v-for="item in roleTreeinfo" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <!--  循环数据结构-->
              <!--
               :data  循环显示数据  //	展示数据
               :props="defaultProps"   //配置选项 children: 'children', label: 'title' 告诉组件对应的显示数据是什么
               :default-expand-all  // 是否展开所有节点
               :expand-on-click-node //是否在点击时展开或者收缩节点
               show-checkbox   // 	节点是否可被选择
               :check-strictly="true"  //在显示复选框的情况下，是否严格的遵循父子不互相关联的做法，默认为 false
                node-key  //标识每个树形结构的id值
              -->
              <el-tree
                ref="treeRef"
                :data="item.children"
                :props="defaultProps"
                :default-expand-all="true"
                :expand-on-click-node="false"
                show-checkbox
                :check-strictly="true"
                node-key="id"
              />
            </div>
          </el-tab-pane>

          <el-tab-pane :label="`配置管理${userInRoleTotal}`" name="roleconfiguration">
            <el-table
              :data="userInRoleIfo"
              style="width: 100%"
            >
              <el-table-column
                prop="id"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </el-tab-pane>

        </el-tabs>

      </div>
    </div>
  </div>
</template>

<script>
import { delRoleInfoApi, roleInfoApi, rolePermissionsInformationApi, roleTreeperisionApi, usersInTheRoleApi } from '@/apis/system'
import { Message } from 'element-ui'
export default {
  name: 'Role',
  data() {
    return {
      // 角色列表
      roleList: [],
      // 页面的鼠标选中下标
      activeIndex: 0,
      // 页面树形数据
      roleTreeinfo: '',
      // 功能列表子数据
      defaultProps: {
        children: 'children',
        label: 'title',
        disabled: 'flag'
      },
      // 每个角色对应的权限数据
      perms: [],
      // tab栏切换对应显示的名称
      activeName: 'roleAdministration',
      // 角色对应的用户列表
      userInRoleIfo: [],
      // 角色对应的用户列表总数
      userInRoleTotal: 0
    }
  },
  async created() {
    // 获取所有角色
    await this.getRoleInfo()
    // 获取所有页面树形功能
    await this.roleTreefn()
    // 获取页面初始的角色信息id
    await this.getrolePermissionsInformationInfo(this.roleList[0].roleId)
    // 获取角色对应的用户列表
    await this.usersInTheRoleFn(this.roleList[0].roleId)
  },
  methods: {
    // 页面获取角色信息
    async getRoleInfo() {
      const res = await roleInfoApi()
      this.roleList = res.data
    },
    // 页面点击选择后赋值
    changeClass(index, roleid) {
      this.activeIndex = index
      // 切换角色后回显对应的树形数据
      this.getrolePermissionsInformationInfo(roleid)
      // 切换角色获取对应的配置用户
      this.usersInTheRoleFn(roleid)
    },
    // 获取所有树形数据信息
    async roleTreefn() {
      const res = await roleTreeperisionApi()
      this.roleTreeinfo = res.data

      // 给所有数据添加disabled属性
      this.roleTreeinfo = this.recursionRoleTreeInfo(this.roleTreeinfo)
    },
    // 递归给所有数组数据添加false属性
    recursionRoleTreeInfo(obj) {
      obj.forEach((item) => {
        item.flag = true
        if (item.children) {
          this.recursionRoleTreeInfo(item.children)
        }
      })
      return obj
    },
    // 获取角色对应所属的权限信息
    async getrolePermissionsInformationInfo(roleid) {
      // 获取对应的角色权限信息
      const res = await rolePermissionsInformationApi(roleid)
      // 将权限信息存储
      this.perms = res.data.perms

      // 设置哪个树形结构是被选中的（组件的内置setCheckedKeys方法通过唯一标识node-key来进行选中）
      // 循环所有的树结构，然后调用树结构内置的setCheckedKeys方法，通过唯一标识node-key来进行选中
      const treeDomList = this.$refs.treeRef
      treeDomList.forEach((item, index) => {
        item.setCheckedKeys(this.perms[index])
      })
    },
    // 获取角色对应的用户列表
    async usersInTheRoleFn(roleId) {
      const res = await usersInTheRoleApi(roleId)
      // console.log(res)
      this.userInRoleIfo = res.data.rows
      this.userInRoleTotal = res.data.total
    },
    // 删除角色
    async edlRolefn(roleId) {
      const res = await delRoleInfoApi(roleId)
      await this.getRoleInfo()
      await this.roleTreefn()
      await this.getrolePermissionsInformationInfo(this.roleList[0].roleId)
      // 获取角色对应的用户列表
      await this.usersInTheRoleFn(this.roleList[0].roleId)
      this.activeIndex = 0
      Message.success(res.msg)
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}

.myELTables {
  width: 100%;
  .myElTabPane {
    display: flex;
  }
}

</style>
